package dao.impl;

import bean.Product;
import dao.BaseDao;
import dao.ProductDao;

import java.util.ArrayList;

public class ProductDaoImpl extends BaseDao implements ProductDao {

    @Override
    public int insert(Product product) {
        String sql = "INSERT INTO `t_product` (`name`,`price`,`category`,`pnum`,`imgurl`,`description`)VALUES(?,?,?,?,?,?)";
        return update(sql,product.getName(),product.getPrice(),product.getCategory(),product.getPnum(),product.getImgurl(),product.getDescription());
    }

    @Override
    public int delete(Integer id) {
        String sql = "DELETE FROM `t_product` WHERE `id`=?";
        return update(sql,id);
    }

    @Override
    public int update(Product product) {
        String sql = "UPDATE `t_product` SET `name`=?,`price`=?,`category`=?,`pnum`=?,`imgurl`=?,`description`=? WHERE `id`=?";
        return update(sql,product.getName(),product.getPrice(),product.getCategory(),product.getPnum(),product.getImgurl(),product.getDescription(),product.getId());
    }

    @Override
    public Product queryById(Integer id) {
        String sql = "SELECT * FROM `t_product` WHERE `id`=?";
        return queryForOne(Product.class,sql,id);
    }

    @Override
    public ArrayList<Product> queryAll() {
        String sql = "SELECT * FROM `t_product`";
        return queryForList(Product.class,sql);
    }
}
